#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import glob
import re

def parse_europa_filename(path) :
  l = os.path.split(path)
  pattern = '[\.-]'
  prefix, year, id_year, lg, suffix = re.split(pattern,l[1])
  return prefix, year, id_year, lg, suffix

def crawl_dir_recur(path, motif, res) :
  expr_all_dir = os.path.join(path, '*')
  pattern_compiled = re.compile(motif)
  for a_path in glob.glob(expr_all_dir) :
    if os.path.isdir(a_path) :
      res = crawl_dir_recur(a_path, motif, res)
    else :
      if pattern_compiled.search(a_path) :
        res.append(a_path)
  return res

def get_set_languages(path_corpus) :
  res = set()
  l = crawl_dir_recur(path_corpus, '.*', [])
  for path in l :
    s = path.split('.')
    res.add(s[-2])
  return res

def id_year2full_year(id_year) :
  if 98 <= id_year <= 99 :
    full_year = int('19%d'%(id_year))
  else :
    full_year = int('20%02d'%(id_year))
  return full_year

def get_nb_doc_lg(a_path, dic_res) :
  expr_all_dir = os.path.join(a_path, '*')
  for a_path in glob.glob(expr_all_dir) :
    if os.path.isdir(a_path) :
      dic_res = get_nb_doc_lg(a_path, dic_res)
#      res = crawl_dir_recur(a_path, res)
    else:
      lg = a_path.split('.')[-2]
      if lg in dic_res.keys() :
        dic_res[lg].append(a_path)
  return dic_res
